package com.yjz.ui.banner.indicator;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.yjz.ui.R;
import com.yjz.ui.util.DisplayUtil;

/**
 * 作用 : 数字指示器
 * 作者 : yjz
 * 时间 : 2024/10/21 16:53
 */
public class NumberIndicator extends FrameLayout implements BannerIndicator<FrameLayout> {

    private final int mPointLeftRightPadding;
    private final int mPointTopBottomPadding;

    public NumberIndicator(@NonNull Context context) {
        this(context, null);
    }

    public NumberIndicator(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumberIndicator(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPointLeftRightPadding = DisplayUtil.dp2px(5f, context.getResources());
        mPointTopBottomPadding = DisplayUtil.dp2px(15f, context.getResources());
    }

    @Override
    public FrameLayout get() {
        return this;
    }

    @Override
    public void onInflate(int count) {

        LinearLayout groupView = new LinearLayout(getContext());
        groupView.setOrientation(LinearLayout.HORIZONTAL);
        groupView.setPadding(0, 0, mPointLeftRightPadding, mPointTopBottomPadding);

        TextView textView = new TextView(getContext());
        textView.setText("1/" + count);
        textView.setTextColor(Color.BLACK);
        textView.setPadding(mPointLeftRightPadding , mPointLeftRightPadding / 2, mPointLeftRightPadding , mPointLeftRightPadding / 2);
        textView.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.number_indicator_bg));
        groupView.addView(textView);

        int VWC = ViewGroup.LayoutParams.WRAP_CONTENT;
        LayoutParams groupViewParams = new LayoutParams(VWC, VWC);
        groupViewParams.gravity = Gravity.END | Gravity.BOTTOM;
        addView(groupView, groupViewParams);
    }

    @Override
    public void onPointChange(int current, int count) {
        ViewGroup viewGroup = (ViewGroup) getChildAt(0);
        TextView indexTv = (TextView) viewGroup.getChildAt(0);
        indexTv.setText(current + 1 + "/" + count);
    }
}
